﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Ws {
    public partial class _ValidateCodeImg : PageSupport {
        protected void Page_Load(object sender, EventArgs e) {
            String code = GenerateCode(4);
            DrawValidateCode(code, 50, 10);
        }
        //绘图
        private void DrawValidateCode(string strValidateCode, int intFgNoise, int intBgNoise) {
            if (strValidateCode == null || strValidateCode.Trim() == String.Empty) {
                return;
            } else {
                //建立一个位图文件 确立长宽
                Bitmap bmpImage = new Bitmap((int)Math.Ceiling((strValidateCode.Length * 12.5)), 18);
                Graphics grpGraphics = Graphics.FromImage(bmpImage);

                try {
                    //生成随机生成器
                    Random rndRandom = new Random();

                    //清空图片背景色
                    grpGraphics.Clear(Color.White);

                    //画图片的背景噪音线
                    for (int i = 0; i < intBgNoise; i++) {
                        int int_x1 = rndRandom.Next(bmpImage.Width);
                        int int_x2 = rndRandom.Next(bmpImage.Width);
                        int int_y1 = rndRandom.Next(bmpImage.Height);
                        int int_y2 = rndRandom.Next(bmpImage.Height);

                        grpGraphics.DrawLine(new Pen(Color.GreenYellow), int_x1, int_y1, int_x2, int_y2);
                    }
                    //把产生的随机数以字体的形式写入画面
                    Font font = new Font("Arial", 11, (FontStyle.Regular | FontStyle.Regular));
                    LinearGradientBrush brhBrush = new LinearGradientBrush(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), Color.Black, Color.Blue, 1.2f, true);
                    grpGraphics.DrawString(strValidateCode, font, brhBrush, 2, 2);

                    //画图片的前景噪音点
                    for (int i = 0; i < intFgNoise; i++) {
                        int int_x = rndRandom.Next(bmpImage.Width);
                        int int_y = rndRandom.Next(bmpImage.Height);

                        bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
                    }

                    //画图片的边框线
                    grpGraphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bmpImage.Width - 1, bmpImage.Height - 1);

                    MemoryStream memsMemoryStream = new MemoryStream();
                    bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
                    Response.ClearContent();
                    Response.ContentType = "image/Gif";
                    Response.BinaryWrite(memsMemoryStream.ToArray());
                } finally {
                    grpGraphics.Dispose();
                    bmpImage.Dispose();
                }
            }
        }

        //取得随机字符串，并设置Session值
        private string GenerateCode(int intLength) {
            int intNumber;
            char chrCode;
            string strValidateCode = String.Empty;

            Random rndRandom = new Random();

            for (int i = 0; i < intLength; i++) {
                intNumber = rndRandom.Next();
                if (intNumber % 2 == 0) {
                    chrCode = (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余
                } else {
                    chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
                }
                strValidateCode += chrCode.ToString();
            }
            Session[ClientBiz.VALIDATE_CODE_SESSION_KEY] = strValidateCode;
            return strValidateCode;
        }
    }
}
